package com.syr.mybatis.policy;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.syr.mybatis.QueryCompare;
import lombok.extern.slf4j.Slf4j;

/**
 * @author sunyiran
 * @since 2021/1/16
 */
@Slf4j
public class GEComparePolicy implements QueryComparePolicy {


    @Override
    public <E, P> void addCompare(QueryWrapper<E> queryWrapper, QueryCompare queryCompare, P p) {

        try {
            Object v = queryCompare.getField().get(p);
            if (queryCompare.isNeedNull()) {
                if (v != null) {
                    queryWrapper.ge(queryCompare.getColumnName(), v);
                } else {
                    throw new RuntimeException("the value can not be null");
                }
            }
        } catch (IllegalAccessException e) {
            log.error("error to add Compare for {}", p.getClass().getSimpleName(), e);
            throw new RuntimeException(e.getMessage());
        }
    }
}
